import React, { Component } from "react"
import { Route, Redirect } from "react-router-dom"
import { hasToken } from "utils/storage"

export default class AuthRoute extends Component {
  render() {
    // console.log(this.props)
    // 把接受到的component属性改成用render进行渲染
    const { component: Component, ...rest } = this.props
    // console.log(rest)
    return (
      <Route
        {...rest}
        render={props => {
          // console.log(props)
          // 这里props是为了获取 history
          if (hasToken()) {
            // 有token就登录
            return <Component {...props}></Component>
          } else {
            // props.history.push("/login")
            // 重定向强制跳转

            return (
              <Redirect
                to={{
                  pathname: "/login",
                  //通过serach传参
                  state: { from: props.location.pathname }
                }}></Redirect>
            )
          }
        }}></Route>
    )
  }
}
